﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

using Main.lib.graphics;
using Main.lib.input;
using Main.lib.util;

namespace Main.lib.scene
{

    public sealed class Scene_Play : Scene_Base
    {

        public Sprite        Player, Ball, Enemy;
        public Text[]        PointsText = {new Text("0"), new Text("0")};
        public BoundingBox[] Bounds;
        public sbyte[]       Direction;
        public int[]         Points = {0, 0};
        public int           Mode;
        public const sbyte   Speed = 2;

        public Scene_Play(byte mode)
        {
            Mode = mode;
        }

        public override void Start()
        {
            Player            = new Sprite(Cache.GetTexture("Wall"));
            Player.Position.Y = (Graphics.Height - Player.Size().Y) / 2;

            Ball            = new Sprite(Cache.GetTexture("Ball"));
            Ball.Position.X = (Graphics.Width - Ball.Size().X) / 2;
            Ball.Position.Y = (Graphics.Height - Ball.Size().Y) / 2;

            Enemy            = new Sprite(Cache.GetTexture("Wall"));
            Enemy.Position.X = Graphics.Width - Enemy.Size().X;
            Enemy.Position.Y = Player.Position.Y;

            PointsText[0].Color = Color.Red;
            PointsText[1].Color = Color.Blue;
            PointsText[1].Position.X = Graphics.Width - PointsText[1].Size().X;

            Bounds    = new BoundingBox[3];
            Bounds[0] = new BoundingBox(new Vector3(Player.Position.X, Player.Position.Y, 0), new Vector3(Player.Position.X + Player.Size().X, Player.Position.Y + Player.Size().Y, 0));
            Bounds[1] = new BoundingBox(new Vector3(Ball.Position.X, Ball.Position.Y, 0),     new Vector3(Ball.Position.X + Ball.Size().X, Ball.Position.Y + Ball.Size().Y, 0));
            Bounds[2] = new BoundingBox(new Vector3(Enemy.Position.X, Enemy.Position.Y, 0),   new Vector3(Enemy.Position.X + Enemy.Size().X, Enemy.Position.Y + Enemy.Size().Y, 0));

            Direction    = new sbyte[2];
            Direction[0] = Speed;
            Direction[1] = Speed;
        }

        public override void Update()
        {
            if (Input.Press(Keys.Escape))
            {
                Scene.Set(new Scene_Title());
                return;
            }

            if (Bounds[1].Intersects(Bounds[0]) || Bounds[1].Intersects(Bounds[2]))
            {
                Direction[0] *= -1;
                if (Bounds[1].Intersects(Bounds[0]))
                    Ball.Position.X = Player.Size().X;
                else
                    Ball.Position.X = Enemy.Position.X - Ball.Size().X;
            }

            if (Ball.Position.X < -Ball.Size().X)
            {
                Points[1]++;
                PointsText[1].Content = Points[1].ToString();
                PointsText[1].Position.X = Graphics.Width - PointsText[1].Size().X;
                Ball.Position.X = (Graphics.Width - Ball.Size().X) / 2;
                Ball.Position.Y = (Graphics.Height - Ball.Size().Y) / 2;
                Direction[0]   *= -1;
                return;
            }
            else if (Ball.Position.X > Graphics.Width)
            {
                Points[0]++;
                PointsText[0].Content = Points[0].ToString();
                Ball.Position.X = (Graphics.Width - Ball.Size().X) / 2;
                Ball.Position.Y = (Graphics.Height - Ball.Size().Y) / 2;
                Direction[0]   *= -1;
                return;
            }

            if ((Ball.Position.Y <= 0) || ((Ball.Position.Y + Ball.Size().Y) > Graphics.Height))
            {
                Direction[1] *= -1;
                if (Ball.Position.Y <= 0)
                    Ball.Position.Y = 0;
                else
                    Ball.Position.Y = Graphics.Height - Ball.Size().Y;
            }

            if (Input.Press(Keys.Q))
            {
                Player.Position.Y -= Speed;
                if (Player.Position.Y < 0)
                    Player.Position.Y = 0;
            }

            if (Input.Press(Keys.A))
            {
                Player.Position.Y += Speed;
                if ((Player.Position.Y + Player.Size().Y) > 480)
                    Player.Position.Y = Graphics.Height - Player.Size().Y;
            }

            if (Mode == 1)
            {
                if (Input.Press(Keys.Up))
                {
                    Enemy.Position.Y -= Speed;
                    if (Enemy.Position.Y < 0)
                        Enemy.Position.Y = 0;
                }

                if (Input.Press(Keys.Down))
                {
                    Enemy.Position.Y += Speed;
                    if ((Enemy.Position.Y + Enemy.Size().Y) > Graphics.Height)
                        Enemy.Position.Y = Graphics.Height - Enemy.Size().Y;
                }
            }
            else
            {
                Enemy.Position.Y = Ball.Position.Y - Enemy.Size().Y / 2;
                if (Enemy.Position.Y < 0)
                    Enemy.Position.Y = 0;
                else if (Enemy.Position.Y > Graphics.Height - Enemy.Size().Y)
                    Enemy.Position.Y = Graphics.Height - Enemy.Size().Y;
            }

            Ball.Position.X += Direction[0];
            Ball.Position.Y += Direction[1];

            Bounds[0].Min.X = Player.Position.X;
            Bounds[0].Min.Y = Player.Position.Y;
            Bounds[0].Max.X = Bounds[0].Min.X + Player.Size().X;
            Bounds[0].Max.Y = Bounds[0].Min.Y + Player.Size().Y;

            Bounds[1].Min.X = Ball.Position.X;
            Bounds[1].Min.Y = Ball.Position.Y;
            Bounds[1].Max.X = Bounds[1].Min.X + Ball.Size().X;
            Bounds[1].Max.Y = Bounds[1].Min.Y + Ball.Size().Y;

            Bounds[2].Min.X = Enemy.Position.X;
            Bounds[2].Min.Y = Enemy.Position.Y;
            Bounds[2].Max.X = Bounds[2].Min.X + Enemy.Size().X;
            Bounds[2].Max.Y = Bounds[2].Min.Y + Enemy.Size().Y;
        }

        public override void Terminate()
        {
            Player.Dispose();
            Ball.Dispose();
            Enemy.Dispose();
            PointsText[0].Dispose();
            PointsText[1].Dispose();
        }

    }

}